package com.example.datadashboard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.datadashboard.entity.DataSource;
import com.example.datadashboard.mapper.DataSourceMapper;
import com.example.datadashboard.service.DataSourceService;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;

@Service
public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements DataSourceService {
    @Override
    public List<DataSource> getUserDataSources(Long userId) {
        QueryWrapper<DataSource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("creator_id", userId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean testConnection(DataSource dataSource) {
        try {
            String url = dataSource.getConnectionConfig();
            Connection connection = DriverManager.getConnection(url);
            connection.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
